package com.yndsoft.jc.ui.memberpage.login;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yndsoft.jc.MainActivity;
import com.yndsoft.jc.R;
import com.yndsoft.jc.activity.BaseActivity;
import com.yndsoft.jc.model.Ad;
import com.yndsoft.jc.model.Member;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.model.SupplierAdmin;
//import com.yndsoft.jc.supplier.main.EntryActivity;
//import com.yndsoft.jc.supplier.main.SignActivity;
import com.yndsoft.jc.supplier.join.JoinActivity;
import com.yndsoft.jc.supplier.main.SupplierActivity;
import com.yndsoft.jc.supplier.utils.IsInputWatcher;
import com.yndsoft.jc.supplier.utils.ToastUtil;
import com.yndsoft.jc.ui.net.util.ProgressSubscriber;
import com.yndsoft.jc.utils.ConstantValues;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private String role;
    private String phone, pwd;
    @Bind(R.id.base_backBtn)
    RelativeLayout base_backBtn;
    @Bind(R.id.base_title)
    TextView base_title;
    @Bind(R.id.login_login)
    Button login_login;
    @Bind(R.id.go_to_register)
    TextView go_to_register;
    @Bind(R.id.login_phone)
    EditText login_phone;
    @Bind(R.id.login_pwd)
    EditText login_pwd;
    @Bind(R.id.login_remove_gray)
    RelativeLayout login_remove_gray;
    @Bind(R.id.forget_psw)
    TextView forget_psw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        role = getIntent().getStringExtra("role");
        if (role == null || role == "") {
            role = "member";
        }
        setViews();
        setListner();
    }

    private void setViews() {
        base_title.setText(getResources().getString(R.string.login));
    }

    private void setListner() {
        base_backBtn.setOnClickListener(this);
        login_phone.addTextChangedListener(isInput);
        login_login.setOnClickListener(this);
        go_to_register.setOnClickListener(this);
        login_remove_gray.setOnClickListener(this);
        forget_psw.setOnClickListener(this);
    }

    private IsInputWatcher isInput = new IsInputWatcher() {
        @Override
        public void noInput() {
            login_remove_gray.setVisibility(View.INVISIBLE);
        }

        @Override
        public void inputting() {
            login_remove_gray.setVisibility(View.VISIBLE);
        }
    };

    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.base_backBtn:
                finish();
                break;
            case R.id.login_login:
                phone = login_phone.getText().toString().trim();
                pwd = login_pwd.getText().toString().trim();
                if (phone == null || "".equals(phone)) {
                    ToastUtil.show(LoginActivity.this, "手机号不能为空！");
                    return;
                }
                if (pwd == null || "".equals(pwd)) {
                    ToastUtil.show(LoginActivity.this, "密码不能为空！");
                    return;
                }
                login();
                break;
            case R.id.go_to_register:
                switch (role) {
                    case "member":
                        intent = new Intent(this, RegisterActivity.class);
                        startActivityForResult(intent, ConstantValues.LOGIN_REQUEST);
                        break;
                    case "supplier":
                        intent = new Intent(this, JoinActivity.class);
                        startActivityForResult(intent, ConstantValues.LOGIN_REQUEST);
                        break;
                    default:
                        break;
                }
                break;
            case R.id.login_remove_gray:
                login_phone.setText("");
                break;
            case R.id.forget_psw:
                intent = new Intent(this, ForgetPswActivity.class);
                intent.putExtra("role", role);
                startActivity(intent);
                finish();
                break;
        }
    }

    public void login() {
        switch (role) {
            case "member":
                //会员登录
                mHttpUtil.memberLogin(new ProgressSubscriber<ReturnMsg>(this) {
                    @Override
                    public void next(ReturnMsg returnMsg) {
                        switch (returnMsg.getCode()) {
                            case 0:
                                ToastUtil.show(LoginActivity.this, "登录失败");
                                break;
                            case 1:
                                ToastUtil.show(LoginActivity.this, "---会员登录成功！---");
                                Type type = new TypeToken<Member>() {
                                }.getType();
                                Object fromJson = new Gson().fromJson(returnMsg.getInfo(), type);
                                Member member = (Member) fromJson;
                                //ToastUtil.show(LoginActivity.this, member.getName());
                                mSession.setRole(1);
                                mSession.setLogin(true);
                                mSession.setUserInfo(member);
                                setResult(RESULT_OK);
                                //回调页面
                                LoginActivity.this.finish();
                                Intent intent = new Intent(mContext, MainActivity.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                startActivity(intent);
                                break;
                        }
                    }
                }, "login", phone, pwd);
                break;
            case "supplier":
                //检测机构管理员登录
                mHttpUtil.supplierLogin(new ProgressSubscriber<ReturnMsg>(this) {
                    @Override
                    public void next(ReturnMsg returnMsg) {
                        switch (returnMsg.getCode()) {
                            case 0:
                                ToastUtil.show(LoginActivity.this, "登录失败");
                                break;
                            case 1:
                                ToastUtil.show(LoginActivity.this, "---检测机构管理员登录成功！---");
                                Type type = new TypeToken<SupplierAdmin>() {
                                }.getType();
                                Object fromJson = new Gson().fromJson(returnMsg.getInfo(), type);
                                SupplierAdmin supplierAdmin = (SupplierAdmin) fromJson;
                                //保存Session
                                mSession.setRole(2);
                                mSession.setLogin(true);
                                mSession.setSuppilerAdmin(supplierAdmin);
                                // 更新数据 start
//                                SharedPreferences sp = getSharedPreferences(getString(R.string.sp_member), Context.MODE_PRIVATE);
//                                SharedPreferences.Editor editor = sp.edit();
//                                editor.putString(getString(R.string.sp_role), "supplier");
//                                editor.putString(getString(R.string.sp_type), supplierAdmin.getTid().toString());
//                                editor.putString(getString(R.string.sp_jc), returnMsg.getInfo());
//                                editor.commit();
                                // 更新数据 end

                                SupplierActivity.start(
                                        LoginActivity.this,
                                        supplierAdmin.getId() + "",
                                        supplierAdmin.getTid(),
                                        supplierAdmin.getSid() + "",
                                        supplierAdmin.getName()
                                );
                                break;
                        }
                    }
                }, "login", phone, pwd);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            setResult(RESULT_OK);
            finish();
        }
    }
}
